/*
 * 文件名： SettingActivity.java
 * 描    述：设置
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.miguo.activity.base.BaseActivity;
import com.miguo.app.AppConfig;
import com.miguo.app.AppContext;
import com.miguo.app.AppHelper;
import com.miguo.component.BaseHeadView;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.SwitchButton;
import com.miguo.plugins.Statistics;
import com.miguo.plugins.UmengPush;
import com.miguo.plugins.UmengUp;
import com.miguo.ui.R;
import com.miguo.utils.BrightnessUtils;
import com.miguo.utils.ClearCache;
import com.miguo.utils.CommonUtils;
import com.miguo.utils.FileUtils;

import java.io.File;

public class SettingActivity extends BaseActivity implements
        OnClickListener {
    private SwitchButton mPush, mNoimg, mNightMode;
    private TextView mSettingVer, mCacheLength;
    private RelativeLayout mGuide, mCheckVersion, mClearData;

    private BaseHeadView mBaseHead;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        mBaseHead = (BaseHeadView) findViewById(R.id.base_head);
        mBaseHead.setActivity(this);
        mBaseHead.setTitle(R.string.setting);
        findViewById();
        initView();
        CheckedChange();
    }

    private void findViewById() {
        // 消息通知
        mPush = (SwitchButton) findViewById(R.id.setting_push);
        mPush.setChecked(UmengPush.isEnabled(mContext));

        // 夜间模式 true false
        mNightMode = (SwitchButton) findViewById(R.id.setting_nightMode);
        mNightMode.setChecked(!AppContext.Prefs().getNightMode());

        // 2G/3G网络时开启无图模式 true false
        mNoimg = (SwitchButton) findViewById(R.id.setting_no_img);
        mNoimg.setChecked(!AppContext.Prefs().getNoImgMode());

        // 版本检测
        mSettingVer = (TextView) findViewById(R.id.setting_ver);
        mSettingVer.setText(CommonUtils.getString(mContext,
                R.string.app_version));
        mCheckVersion = (RelativeLayout) findViewById(R.id.setting_checkVersion);
        mCheckVersion.setOnClickListener(this);

        // 清除缓存
        mCacheLength = (TextView) findViewById(R.id.setting_cache_length);
        mClearData = (RelativeLayout) findViewById(R.id.setting_clear_data);
        mClearData.setOnClickListener(this);

        // 用户反馈
//        findViewById(R.id.setting_feedback).setOnClickListener(this);

        // 关于我们
        mGuide = (RelativeLayout) findViewById(R.id.setting_guide);
        mGuide.setOnClickListener(this);
    }

    /**
     * 计算缓存大小
     */
    private void initView() {
        long fileSize = 0;
        String cacheSize = "0KB";
        File filesDir = this.getFilesDir();
        File cacheDir = this.getCacheDir();

        fileSize += FileUtils.getDirSize(filesDir);
        fileSize += FileUtils.getDirSize(cacheDir);
        // 2.2版本才有将应用缓存转移到sd卡的功能
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
            File externalCacheDir = mContext.getExternalCacheDir();
            fileSize += FileUtils.getDirSize(externalCacheDir);
        }
        if (fileSize > 1048576) {
            cacheSize = FileUtils.formatFileSize(fileSize);
            mCacheLength.setText("清除缓存(" + cacheSize + ")");
        } else {
            mCacheLength.setText("清除缓存");
        }
    }

    /**
     * 设置3个开关
     */
    private void CheckedChange() {
        mNoimg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if (isChecked) {
                    AppContext.isNoImg = false;
                    AppContext.Prefs().setNoImgMode(false);
                    Statistics.onTrackCustomClick(mContext,
                            AppConfig.STATISTICS.EVENT_NAME_19);
                    return;
                } else {
                    AppContext.isNoImg = true;
                    AppContext.Prefs().setNoImgMode(true);
                    Statistics.onTrackCustomClick(mContext,
                            AppConfig.STATISTICS.EVENT_NAME_18);
                }

            }
        });

        mPush.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if (isChecked) {
                    UmengPush.disable(mContext);
                    Statistics.onTrackCustomClick(mContext,
                            AppConfig.STATISTICS.EVENT_NAME_16);
                } else {
                    UmengPush.enable(mContext);
                    Statistics.onTrackCustomClick(mContext,
                            AppConfig.STATISTICS.EVENT_NAME_17);
                }
            }
        });

        mNightMode.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if (isChecked) {
                    AppContext.isOpenNight = false;
                    BrightnessUtils.setNightMode(mContext, false);
                    AppContext.Prefs().setNightMode(false);
                    Statistics.onTrackCustomClick(mContext,
                            AppConfig.STATISTICS.EVENT_NAME_20);
                } else {
                    AppContext.isOpenNight = true;
                    BrightnessUtils.setNightMode(mContext, true);
                    AppContext.Prefs().setNightMode(true);
                    Statistics.onTrackCustomClick(mContext,
                            AppConfig.STATISTICS.EVENT_NAME_21);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            // 版本检测
            case R.id.setting_checkVersion:
                Toast.show(mContext, "正在检测新版本,请稍侯...");
                UmengUp.forceUpdate(mContext);
                Statistics.onTrackCustomClick(mContext,
                        AppConfig.STATISTICS.EVENT_NAME_22);
                break;
            // 清除缓存
            case R.id.setting_clear_data:
                Toast.show(mContext, "正在清除缓存,请稍候...");
                ClearCache.App();
                mCacheLength.setText("清除缓存");
                Statistics.onTrackCustomClick(mContext,
                        AppConfig.STATISTICS.EVENT_NAME_23);
                break;
//            // 意见反馈
//            case R.id.setting_feedback:
//                UmengFeedback.startFeedbackActivity(mContext);
//                Statistics.onTrackCustomClick(mContext,
//                        AppConfig.STATISTICS.EVENT_NAME_15);
//                break;
            // 关于我们
            case R.id.setting_guide:
                AppHelper.StartAbout(this);
                break;
        }
    }

}
